include ../upgradeable_versions.mk

EXTENSION    = postgis_tiger_geocoder
EXTVERSION    = @POSTGIS_LIB_VERSION@
MINORVERSION  = 2011.@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
GREP = @GREP@
PERL = @PERL@

MICRO_NUMBER  = $(shell echo $(EXTVERSION) | \
						$(PERL) -pe 's/\d.\d.(\d+)[a-zA-Z]*\d*/$1/'

PREREL_NUMBER = $(shell echo $(EXTVERSION) | \
                        $(PERL) -pe 's/\d\.\d\.(.*)/\1/' | \
                        $(GREP) "[a-zA-Z]" | \
                        $(PERL) -pe 's/\d+[a-zA-Z]+(\d+)/\1/'

MICRO_PREV    = $(shell if test "$(MICRO_NUMBER)x" != "x"; then expr $(MICRO_NUMBER) - 1; fi)
PREREL_PREV   = $(shell if test "$(PREREL_NUMBER)x" != "x"; then expr $(PREREL_NUMBER) - 1; fi)

PREREL_PREFIX = $(shell echo $(EXTVERSION) | \
                        $(PERL) -pe 's/\d\.\d\.(.*)/\1/' | \
                        $(GREP) "[a-zA-Z]" | \
                        $(PERL) -pe 's/(\d+[a-zA-Z]+)\d*/\1/'

DATA_built = \
	$(EXTENSION).control \
	sql/$(EXTENSION)--$(EXTVERSION).sql \
	sql/$(EXTENSION)--unpackaged--$(EXTVERSION).sql \
	$(NULL)

REGRESS = test-normalize_address test-upgrade
REGRESS_OPTS = --load-extension=fuzzystrmatch --load-extension=postgis --load-extension=$(EXTENSION)

SQL_BITS     = $(wildcard sql_bits/*.sql)
EXTRA_CLEAN = sql ${SQL_BITS}

#test address standardizer if also built
ifeq (@ADDRESS_STANDARDIZER@,address_standardizer)
	REGRESS +=  test-pagc_normalize_address
	REGRESS_OPTS += --load-extension=address_standardizer
endif

all: sql/$(EXTENSION)--$(EXTVERSION).sql sql/$(EXTENSION)--unpackaged--$(EXTVERSION).sql sql/$(EXTENSION)--ANY--$(EXTVERSION).sql sql/test-normalize_address.sql sql/test-pagc_normalize_address.sql expected/test-normalize_address.out expected/test-pagc_normalize_address.out sql/test-upgrade.sql expected/test-upgrade.out


expected:
	mkdir -p $@

sql:
	mkdir -p $@

sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql | sql
	cp $< $@

expected/test-normalize_address.out: sql_bits/test_tuples_only_unaligned.sql.in ../../extras/tiger_geocoder/regress/normalize_address_regress | expected
	cat $^ > $@

expected/test-pagc_normalize_address.out: sql_bits/test_tuples_only_unaligned.sql.in ../../extras/tiger_geocoder/regress/pagc_normalize_address_regress | expected
	cat $^ > $@

## adding \t .. to force output to be the same as what get in regular regress in tiger folder
sql/test-normalize_address.sql: sql_bits/test_tuples_only_unaligned.sql.in ../../extras/tiger_geocoder/regress/normalize_address_regress.sql | sql
	cat $^ > $@

sql/test-upgrade.sql: | sql
	echo 'ALTER EXTENSION ${EXTENSION} UPDATE TO "$(EXTVERSION)next"' > $@

%.control: %.control.in Makefile
	cat $< \
		| sed -e 's|@EXTVERSION@|$(EXTVERSION)|g' \
		> $@

expected/test-upgrade.out: sql/test-upgrade.sql | expected
	cp $< $@

sql/test-pagc_normalize_address.sql: sql_bits/test_tuples_only_unaligned.sql.in ../../extras/tiger_geocoder/regress/pagc_normalize_address_regress.sql | sql
	cat $^ > $@

sql/$(EXTENSION).sql: sql_bits/tiger_geocoder.sql  sql_bits/mark_editable_objects.sql.in sql_bits/tiger_geocoder_comments.sql | sql
	cat $^ > $@

sql/$(EXTENSION)--ANY--$(EXTVERSION).sql: ../postgis_extension_helper.sql sql_bits/remove_from_extension.sql.in sql/tiger_geocoder_upgrade_minor.sql sql_bits/mark_editable_objects.sql.in sql_bits/tiger_geocoder_comments.sql | sql
	cat $^ > $@
	echo "SELECT postgis_extension_drop_if_exists('${EXTENSION}', 'DROP SCHEMA tiger_data');" >> $@
	cat ../postgis_extension_helper_uninstall.sql  >> $@

sql/$(EXTENSION)--$(EXTVERSION)next--$(EXTVERSION).sql: sql/$(EXTENSION)--$(EXTVERSION)--$(EXTVERSION)next.sql | sql
	cp $< $@

#strip BEGIN/COMMIT since these are not allowed in extensions
#strip CREATE SCHEMA since we force extension
# to create schema by setting schema to tiger_geocoder in control
#also remove tiger_data from extension if it is part of it
sql_bits/tiger_geocoder_minor.sql.in: ../../extras/tiger_geocoder/utility/set_search_path.sql \
  sql_bits/upgrade_before.sql.in \
  	../../extras/tiger_geocoder/geocode_settings.sql \
  ../../extras/tiger_geocoder/tiger_loader_2020.sql \
	../../extras/tiger_geocoder/utility/utmzone.sql \
	../../extras/tiger_geocoder/utility/cull_null.sql \
	../../extras/tiger_geocoder/utility/nullable_levenshtein.sql \
	../../extras/tiger_geocoder/utility/levenshtein_ignore_case.sql \
	../../extras/tiger_geocoder/normalize/end_soundex.sql \
	../../extras/tiger_geocoder/normalize/count_words.sql \
	../../extras/tiger_geocoder/normalize/state_extract.sql \
	../../extras/tiger_geocoder/normalize/get_last_words.sql \
	../../extras/tiger_geocoder/normalize/location_extract_countysub_exact.sql \
	../../extras/tiger_geocoder/normalize/location_extract_countysub_fuzzy.sql \
	../../extras/tiger_geocoder/normalize/location_extract_place_exact.sql \
	../../extras/tiger_geocoder/normalize/location_extract_place_fuzzy.sql \
	../../extras/tiger_geocoder/normalize/location_extract.sql \
	../../extras/tiger_geocoder/normalize/normalize_address.sql \
	../../extras/tiger_geocoder/normalize/pprint_addy.sql \
	../../extras/tiger_geocoder/pagc_normalize/pagc_tables.sql \
  ../../extras/tiger_geocoder/pagc_normalize/pagc_normalize_address.sql \
	../../extras/tiger_geocoder/geocode/other_helper_functions.sql \
	../../extras/tiger_geocoder/geocode/rate_attributes.sql \
	../../extras/tiger_geocoder/geocode/includes_address.sql \
	../../extras/tiger_geocoder/geocode/interpolate_from_address.sql \
	../../extras/tiger_geocoder/geocode/geocode_address.sql \
	../../extras/tiger_geocoder/geocode/geocode_location.sql \
	../../extras/tiger_geocoder/geocode/geocode_intersection.sql \
	../../extras/tiger_geocoder/geocode/geocode.sql \
	../../extras/tiger_geocoder/geocode/reverse_geocode.sql \
	../../extras/tiger_geocoder/geocode/census_tracts_functions.sql
	mkdir -p sql_bits
	cat $^ > $@

#also remove tiger_data from extension if it is part of it so data gets backed up
sql_bits/tiger_geocoder.sql.in: sql_bits/norm_addy_create.sql.in \
	../../extras/tiger_geocoder/utility/set_search_path.sql \
	../../extras/tiger_geocoder/geocode_settings.sql \
	../../extras/tiger_geocoder/tables/lookup_tables_2011.sql \
	../../extras/tiger_geocoder/tiger_loader_2020.sql \
	../../extras/tiger_geocoder/utility/set_search_path.sql \
	../../extras/tiger_geocoder/utility/utmzone.sql \
	../../extras/tiger_geocoder/utility/cull_null.sql \
	../../extras/tiger_geocoder/utility/nullable_levenshtein.sql \
	../../extras/tiger_geocoder/utility/levenshtein_ignore_case.sql \
	../../extras/tiger_geocoder/normalize/end_soundex.sql \
	../../extras/tiger_geocoder/normalize/count_words.sql \
	../../extras/tiger_geocoder/normalize/state_extract.sql \
	../../extras/tiger_geocoder/normalize/get_last_words.sql \
	../../extras/tiger_geocoder/normalize/location_extract_countysub_exact.sql \
	../../extras/tiger_geocoder/normalize/location_extract_countysub_fuzzy.sql \
	../../extras/tiger_geocoder/normalize/location_extract_place_exact.sql \
	../../extras/tiger_geocoder/normalize/location_extract_place_fuzzy.sql \
	../../extras/tiger_geocoder/normalize/location_extract.sql \
	../../extras/tiger_geocoder/normalize/normalize_address.sql \
	../../extras/tiger_geocoder/normalize/pprint_addy.sql \
	../../extras/tiger_geocoder/pagc_normalize/pagc_tables.sql \
    ../../extras/tiger_geocoder/pagc_normalize/pagc_normalize_address.sql \
	../../extras/tiger_geocoder/geocode/other_helper_functions.sql \
	../../extras/tiger_geocoder/geocode/rate_attributes.sql \
	../../extras/tiger_geocoder/geocode/includes_address.sql \
	../../extras/tiger_geocoder/geocode/interpolate_from_address.sql \
	../../extras/tiger_geocoder/geocode/geocode_address.sql \
	../../extras/tiger_geocoder/geocode/geocode_location.sql \
	../../extras/tiger_geocoder/geocode/geocode_intersection.sql \
	../../extras/tiger_geocoder/geocode/geocode.sql \
	../../extras/tiger_geocoder/geocode/reverse_geocode.sql \
	../../extras/tiger_geocoder/geocode/census_tracts_functions.sql \
	../../extras/tiger_geocoder/topology/tiger_topology_loader.sql \
	../postgis_extension_helper.sql \
	sql_bits/add_search_path.sql
	mkdir -p sql_bits
	cat $^ > $@
	echo "SELECT postgis_extension_drop_if_exists('${EXTENSION}', 'DROP SCHEMA tiger_data');" >> $@
	cat ../postgis_extension_helper_uninstall.sql  >> $@

sql_bits/tiger_geocoder.sql: sql_bits/tiger_geocoder.sql.in
	mkdir -p sql_bits
	sed -e 's/BEGIN;//g' -e 's/COMMIT;//g' -e '/^CREATE SCHEMA/d;'  $< > $@
	$(PERL) -pe 's/BEGIN\;//g ; s/COMMIT\;//g' $< > $@

sql_bits/add_search_path.sql: sql_bits/add_search_path.sql.in
	mkdir -p sql_bits
	cp $< $@

../../doc/tiger_geocoder_comments.sql:
	$(MAKE) -C ../../doc comments

sql_bits/tiger_geocoder_comments.sql: ../../doc/tiger_geocoder_comments.sql
	mkdir -p sql_bits
	cp $< $@

#grep all lines that start with CREATE OR REPLACE FUNCTION, TRIGGER...
#then replace CREATE OR REPLACE .. with ALTER EXTENSION..;
#then remove default values and extra junk
# sql/$(EXTENSION)--unpackaged--$(EXTVERSION).sql: ../../tiger_geocoder/tiger_geocoder.sql
# 	sed -e '/^CREATE \(OR REPLACE\|TYPE\|TABLE\|VIEW\|CAST\)/!d;' \
# 		-e 's/OR REPLACE//g' \
# 		-e 's/CREATE\(.*\)/ALTER EXTENSION $(EXTENSION) ADD\1;/' \
# 		-e 's/DEFAULT [\.0-9a-zA-Z]\+//g' \
# 		-e 's/\(BEFORE\|WITH FUNCTION\)\(.*\)/;/' \
# 		-e 's/[ \t]+;/;/' \
# 		-e 's/(;/;/' \
# 		-e 's/\\(;/;/' \
# 		-e 's/;;/;/g' $< > $@

#hardcode for now
#TODO: generate via create_unpackaged.pl
sql/$(EXTENSION)--unpackaged--$(EXTVERSION).sql: sql_bits/tiger_geocoder--unpackaged.sql.in | sql
	cp $< $@

#upgrade script should have everything but table, schema, type creation/alter
#NOTE: we assume all object definitions end in ;
#first expression deletes all non-removable objects defined on same line
#second deletes all non-removable defined on multiple lines
#  the end of the body of object we assume ends in ;
#aggregates are special
#they can be dropped but we need to remove
#them from the extension first
sql/tiger_geocoder_upgrade_minor.sql:  sql_bits/tiger_geocoder_minor.sql.in | sql
	sed -e '/^\(CREATE\|ALTER\) \(CAST\|TYPE\|TABLE\|SCHEMA\|DOMAIN\|TRIGGER\).*;/d' \
	 	 -e '/^\(CREATE\|ALTER\) \(CAST\|TYPE\|TABLE\|SCHEMA\|DOMAIN\|TRIGGER\)/,/\;/d' \
	 	 -e 's/BEGIN;//g' -e 's/COMMIT;//g' \
	 	 $< > $@

EXTDIR=$(DESTDIR)$(datadir)/$(datamoduledir)

install: install-upgrade-paths

# The "next" lines are a cludge to allow upgrading between different
# revisions of the same version
install-upgrade-paths:
	tpl='$(EXTENSION)--ANY--$(EXTVERSION).sql'; \
	$(INSTALL_DATA) sql/$${tpl} "$(EXTDIR)/$${tpl}"; \
	ln -fs "$${tpl}" $(EXTDIR)/$(EXTENSION)--$(EXTVERSION)--$(EXTVERSION)next.sql; \
	ln -fs "$${tpl}" $(EXTDIR)/$(EXTENSION)--$(EXTVERSION)next--$(EXTVERSION).sql; \
	for OLD_VERSION in $(UPGRADEABLE_VERSIONS); do \
		ln -fs "$${tpl}" $(EXTDIR)/$(EXTENSION)--$$OLD_VERSION--$(EXTVERSION).sql; \
	done

uninstall: uninstall-upgrade-paths

INSTALLED_UPGRADE_SCRIPTS = \
	$(wildcard $(EXTDIR)/*$(EXTVERSION).sql) \
	$(wildcard $(EXTDIR)/*$(EXTVERSION)next.sql) \
	$(NULL)

uninstall-upgrade-paths:
	rm -f $(INSTALLED_UPGRADE_SCRIPTS)

#only extension files
EXTRA_CLEAN += $(wildcard expected/*--*.out)

distclean: clean
	rm Makefile

PG_CONFIG := @PG_CONFIG@
PGXS := @PGXS@
include $(PGXS)
PERL=@PERL@
